/**
 * Created by huachuan on 2015/12/26.
 */
angular.module('newsservices', [])
  .factory('News', function($resource,$rootScope,ENV) {
    //访问的地址
    var APIUrl = ENV.api;
    var newsTopListJson,newsContentJson,collectNewsJson,likeNewsJson,messageIdList,fromWhereJson;

    var newsBottomListJson,pageSize=10;//获取底部资讯的分页
    //获取$resource
    var newsResource = $resource(APIUrl+'message/getMessageList.do');
    var newsDetailResource = $resource(APIUrl+'message/getMessageDetailById.do');
    var colletNewsResource = $resource(APIUrl+"message/addMessageCollect.do");
    var likeNewsResource = $resource(APIUrl+"message/likeMessage.do");
    return{
      //保存从何处来的
      saveFromWhere:function(fromWhere){
        fromWhereJson = fromWhere;
      },
      getFromWhere:function(){
        return fromWhereJson;
      },
      //获取到资讯的编号
      getMessageIdList:function(){
        return messageIdList;
      },
      //获取的新闻信息
      getNewsList:function(tip){
          var hasNextPage = true;
           newsResource.save({
             tip:tip,
            pageIndex:1,
            pageSize:pageSize
          },function(response){
            if(response.bottomMessage.length<pageSize){
              hasNextPage = false;
            }
             messageIdList = response.messageIdList;
             if(tip!='noTop'){
               newsTopListJson = response.topMessage;
             }
            newsBottomListJson = {
              hasNextPage:hasNextPage,
              nextPage: 2,
              data: response.bottomMessage
            };
            $rootScope.$broadcast('News.newsUpdated');
          });
      },
      //加载更多资讯
      getMoreNewsList:function(){
        if(newsBottomListJson===undefined){
          return false;
        }
        var hasNextPage=newsBottomListJson.hasNextPage;
        var nextPage=newsBottomListJson.nextPage;
        var moreMessageData=newsBottomListJson.data;
        if(!hasNextPage){
          return false;
        }
        newsResource.save({
          pageIndex:nextPage,
          pageSize:pageSize
        },function(response){
          nextPage++;
          if(response.bottomMessage.length<pageSize){
            hasNextPage = false;
          }
          moreMessageData = moreMessageData.concat(response.bottomMessage);
          newsBottomListJson = {
            hasNextPage:hasNextPage,
            nextPage: nextPage,
            data: moreMessageData
          };
          $rootScope.$broadcast('News.MoreNewsUpdated');
        });
      },
      //是否有下一页
      hasNextPage: function() {
        if (newsBottomListJson === undefined) {
          return false;
        }
        return newsBottomListJson.hasNextPage;
      },
      getTopNewsListJson:function(){
        return newsTopListJson;
      },
      getBottomNewsListJson:function(){
        return newsBottomListJson.data;
      },
      //获取新闻的具体内容
      getNewsContent:function(messageId,memberId){
        return newsDetailResource.save({
          message_id:messageId,
          member_id:memberId
        },function(response){
            newsContentJson = response;
          $rootScope.$broadcast('News.newsDetailUpdated');
        });
      },
      //收藏该资讯
      collectNews:function(memberId,messageId){
        colletNewsResource.save({
          member_id:memberId,
          mc_obj_id:messageId
        },function(response){
          collectNewsJson = response;
          $rootScope.$broadcast('News.collectNewsUpdated');
        });
      },
      //点赞该资讯
      likeNews:function(memberId,messageId){
        likeNewsResource.save({
          member_id:memberId,
          message_id:messageId
        },function(response){
          likeNewsJson = response;
          $rootScope.$broadcast('News.likeNewsUpdated');
        });
      },
      //返回获取到的信息json信息
      getNewsJson:function(){
        return newsListJson;
      },
      //返回新闻的具体信息
      getContentJson:function(){
        return newsContentJson;
      },
      //返回收藏的信息
      getCollectJson:function(){
        return collectNewsJson;
      },
      //返回点赞的信息
      getLikeJson:function(){
        return likeNewsJson;
      }

    }
  })



//评论的Service


  .factory('NewsComment', function($resource,$rootScope,ENV) {
    var APIUrl = ENV.api;

    var newsCommentJson,messageInfo,memberInfo,messageTitle,deleteCommentJson;
    var commentListJson,pageSize=15;
    var newsCommentResource = $resource(APIUrl+"message/getMessageCommentList.do");
    var publishCommentResource = $resource(APIUrl+"message/addMessageComment.do");
    var commentCountResource = $resource(APIUrl+"message/getCommentCount.do");
    var deleteCommentResource = $resource(APIUrl+"message/delMessageComment.do");

    //获取的新闻的评论列表
    return {
      //删除评论
      deleteComment:function(mcommentId,memberId,messageId){
        deleteCommentResource.save({
          mcomment_id:mcommentId,
          member_id:memberId,
          message_id:messageId
        },function(response){
          deleteCommentJson = response;
          $rootScope.$broadcast('NewsComment.deleteCommentUpdated');
        });
      },
      getDelCommentJson:function(){
        return deleteCommentJson;
      },
      getCommentList:function(messageId,memberId){
        var hasNextPage = true;
        newsCommentResource.save({
          message_id:messageId,
          member_id:memberId,
          pageIndex:1,
          pageSize:pageSize
        },function(response){
          if(response.newsCommentList.length<pageSize){
            hasNextPage = false;
          }
          memberInfo = response.memberInfo;
          messageTitle = response.messageTitle;
          commentListJson = {
            hasNextPage:hasNextPage,
            nextPage: 2,
            data: response.newsCommentList
          };
          $rootScope.$broadcast('News.detailCommentUpdated');
        });
      },
      getMoreCommentList:function(messageId,memberId){
        if(commentListJson===undefined){
          return false;
        }
        var hasNextPage=commentListJson.hasNextPage;
        var nextPage=commentListJson.nextPage;
        var moreCommentData=commentListJson.data;
        if(!hasNextPage){
          return false;
        }
        newsCommentResource.save({
          message_id:messageId,
          member_id:memberId,
          pageIndex:nextPage,
          pageSize:pageSize
        },function(response){
          nextPage++;
          if(response.newsCommentList.length<pageSize){
            hasNextPage = false;
          }
          moreCommentData = moreCommentData.concat(response.newsCommentList);
          commentListJson = {
            hasNextPage:hasNextPage,
            nextPage: nextPage,
            data: moreCommentData
          };
          $rootScope.$broadcast('News.MoreDetailCommentUpdated');
        });
      },
      //是否有下一页
      hasNextPage: function() {
        if (commentListJson === undefined) {
          return false;
        }
        return commentListJson.hasNextPage;
      },
      //获取到资讯的标题
      getMessageTitleJson:function(){
        return messageTitle;
      },
      getCommentCount:function(messageId,memberId){
        commentCountResource.save({
          message_id:messageId,
          member_id:memberId
        },function(reponse){
          messageInfo = reponse;
          $rootScope.$broadcast('News.CommentCountUpdated');
        })
      },
      //发表评论
      publishComment:function(memberId,messageId,newsComment){
        publishCommentResource.save({
          member_id:memberId,
          message_id:messageId,
          mcomment_content:newsComment
        },function(response){
          newsCommentJson = response;
          $rootScope.$broadcast('News.publishCommentUpdated');
        });
      },
      getCommentJson:function(){
        if(commentListJson===undefined){
          return false;
        }
        return commentListJson.data;
      },
      getMemberInfo:function(){
        return memberInfo;
      },
      //获取到新增的评论内容
      getNewComment:function(){
        return newsCommentJson;
      },
      //获取评论的数量
      getCommetCountJson:function(){
        return messageInfo;
      }
    }
  })
;

